define(function(require){
    $(function(){
    	/*---------- 流量资源——供应商/商铺运营人员 ----------*/
    	
    	/* 表格 */
    	var bodyTemplate = ['<tr traffic_id="{traffic_id}">',
    	                    '<td class="txtc"><a href="/trp/resource/main/?traffic={traffic_id}" title="{position_name}">{position_name}</a></td>',
    	                    '<td class="txtc position_attrs">{position_attrs}</td>',
    	                    '<td class="txtc position_width">{width}</td>',
    	                    '<td class="txtc position_height">{height}</td>',
    	                    '<td class="txtc" max="{max}">{max}</td>',
    	                    '<td class="txtc media_name">{media_name}</td>',
    	                    '<td class="txtc media_attrs">{media_attrs}</td>',
    	                    '<td class="txtc circle" start="{start}" end="{end}">{start} 至 {end}</td>',
    	                    '<td class="txtc pv">{pv}</td>',
    	                    '<td class="txtc click">{click}</td>',
    	                    '<td class="txtc">{floor}</td>',
	                     	'<td class="txtc">',
	                     		/*'<a class="ibtn showchart" href="javascript:;" title="查看报表"><span class="glyphicon glyphicon-list-alt"></span></a>',*/
	                     		'<a class="ibtn relatedbtn" href="/trp/strategy/?traffic_id={traffic_id}" title="关联策略"><span class="glyphicon glyphicon-th-list"></span></a>',
                     		'</td>',
	                     '</tr>'].join('');
        
    	var date = $('[name=reservation]').val().split(' 至 ');
        var grid = Grid.init({
	            holder: 'trafficList',
	            bodyTemplate: bodyTemplate,
	            dataSource: '/trp/traffic/get_list',
	            pageTemplate: semi.grid.pageTemplate,
	            sendOnPageSize: true
	        });
        
        grid.onfilled(function(data){
        	grid.data = data;
        	$('.ibtn').tooltip();
        });
        
        /* 数据搜索 */
        var filterData = function(pno){
        	var txt = $('.searchbox-ipt').val(),
            	d = $('[name=reservation]').val().split(' 至 '),
            	size = $('.pageheader [name=measure]').val(),
            	width = '', height = '',
            	pageSize = 20;
        	
        	if(!semi.utils.isEmpty(size)){
	    		size = size.split('x');
	    		width = size[0];
	    		height = size[1];
	    	}
        	if($('select.page-size').size()>0){
        		pageSize = $('select.page-size').val();
        	}
            grid.send({
        		search_text: txt,
        		start: d[0],
        		end: d[1],
        		width: width,
	        	height: height,
        		pageSize: pageSize,
        		pageNo: pno || 1
        	});
        };
        filterData();
        
        /* 日期选择 */
        $('input[name=reservation]').daterangepicker({
        	opens: 'left',
            format:'YYYY-MM-DD'
        }, function(start, end, label) {
        	console.log(start.toISOString(), end.toISOString(), label);
        	filterData();
        });
        
        //按名称、属性模糊搜索
        $('.pageheader .searchbox-btn').on('click', function(){
        	filterData();
        });
        $('.pageheader .searchbox-ipt').on('keyup', function(e){
        	if(e.keyCode==semi.keyCode.ENTER){
        		filterData();
        	}
        });
        
        //按尺寸搜索
        $('.pageheader [name=measure]').select2({
        	ajax: {
        		url: '/trp/position/get_position_size/',
        		results: function(data){
        			var resultArr = [{id:'', text:'全部尺寸'}];
        			$.each(data.data, function(i,n){
        				var val = n.width+'x'+n.height;
        				resultArr.push({id:val, text:val});
        			});
        			return {results: resultArr}
        		}
        	}
        }).on('change', function(){
        	filterData();
        });
        
        /* 查看报表-dialog */
        var $popchart = require('./common/popchart');
        $(document).on('click', '.showchart', function(){
        	var $this = $(this), $tr = $this.parents('tr:first'), traffic_id = $tr.attr('traffic_id');
        	$popchart({
            	title: '广告位流量报表',
            	btn: $this,
            	dataurl: '/trp/traffic/get_report',
            	data: {
            		traffic_id: traffic_id
            	}
            });
        });
        
    });
});